/* eslint-disable import/no-anonymous-default-export */
import { memberLevelList, addMemberLevel, imgs, delLevel, updateMemberLevel } from '../services/level'

export default {
    state: {
        data: [],
        addMemberLevelData: [],
        imgData: [],
        delLevelData: []
    },
    effects: {
        async memberLevelList(params) {
            const response = await memberLevelList(params)

            if (response.code === 200) {
                this.saveMemberLevelList({
                    data: response.data
                })
            }
        },
        async addMemberLevel(params) {
            const response = await addMemberLevel(params)

            if (response.code === 200) {
                this.saveAddMemberLevel({
                    addMemberLevelData: response.data
                })
            }
        },
        async imgs(params) {
            const response = await imgs(params)
            if (response.code === 200) {

                this.saveImg({
                    imgData: response.data[0].relativePath
                })

            }
            return response
        },
        async delLevel(params) {
            const response = await delLevel(params)
            if (response.code === 200) {

                this.saveDelLevel({
                    delLevelData: response.data
                })

            }
        },
        async updateMemberLevel(params) {
            const response = await updateMemberLevel(params)
            if (response.code === 200) {

                this.saveUpdateMemberLevel({
                    updateMemberLevelData: response.data
                })

            }
        },
    },
    reducers: {
        saveMemberLevelList(state, paylode) {
            return {
                ...state,
                ...paylode
            }
        },
        saveAddMemberLevel(state, paylode) {
            return {
                ...state,
                ...paylode
            }
        },
        saveImg(state, payload) {
            return {
                ...state,
                ...payload
            }
        },
        saveDelLevel(state, payload) {
            return {
                ...state,
                ...payload
            }
        },
        saveUpdateMemberLevel(state, payload) {
            return {
                ...state,
                ...payload
            }
        },
        clear(state, payload) {
            return {
                ...state,
                imgData: [],
            }
        },
    }
}